*&---------------------------------------------------------------------*
*& 包含               ZFI005F01
*&---------------------------------------------------------------------*

*--------------------------------------------------------------------*
* 选择屏幕
*--------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
  PARAMETERS: p_file LIKE rlgrap-filename MODIF ID m1.  " 文件上传
  SELECTION-SCREEN FUNCTION KEY 1.
SELECTION-SCREEN END OF BLOCK b1.

*----------------------------------------------------------------------*
*          AT SELECTION-SCREEN)                                        *
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM file_input CHANGING p_file .

AT SELECTION-SCREEN.
  IF sscrfields-ucomm = 'FC01'.
    PERFORM downdload_template USING 'ZFIR005' '会计凭证导入模板'. " 放下载模板
  ENDIF.
*&---------------------------------------------------------------------*
*& Form file_input
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      <-- P_FILE
*&---------------------------------------------------------------------*
FORM file_input  CHANGING p_p_file.
  DATA:lt_tab         TYPE filetable,         "存放文件名的内表
       lv_file_filter TYPE string,            "file filter
       lv_rc          TYPE i.                 "函数返回值

  REFRESH:lt_tab.
  CLEAR:lv_file_filter,lv_rc.

  lv_file_filter = 'EXCEL文件|*.XLS;*.XLSX'.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title            = '选择数据文件'
      file_filter             = lv_file_filter
*     initial_directory       = 'C:\'
    CHANGING
      file_table              = lt_tab
      rc                      = lv_rc
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.

  IF sy-subrc = 0 AND lv_rc = 1.
    READ TABLE lt_tab INTO p_file INDEX 1.
  ELSE.
    MESSAGE '没有选择文件或是选择文件错误！' TYPE 'S'.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form downdload_template
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> P_
*&      --> P_
*&---------------------------------------------------------------------*
FORM downdload_template USING p_objid TYPE wwwdatatab-objid
      p_filename TYPE string.

  DATA: w_objdata     LIKE wwwdatatab,
        w_mime        LIKE w3mime,
        "w_filename    TYPE string,
        w_fullpath    TYPE string VALUE 'C:/TEMP/',
        w_path        TYPE string VALUE 'C:/TEMP/',
        w_destination LIKE rlgrap-filename,
        w_objnam      TYPE string,
        w_rc          LIKE sy-subrc,
        w_errtxt      TYPE string,
        p_dest        LIKE sapb-sappfad,
        w_action      TYPE i.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title         = '下载导入模板'
      default_extension    = 'XLS;XLSX'
      default_file_name    = p_filename
      file_filter          = 'EXCEL文件|*.XLSX;*.XLS'
    CHANGING
      filename             = p_filename
      path                 = w_path
      fullpath             = w_fullpath
      user_action          = w_action
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.

  IF sy-subrc = 0 AND w_action EQ 0.
    p_dest = w_fullpath.
    CONDENSE w_objnam NO-GAPS.
    SELECT SINGLE relid objid
    INTO CORRESPONDING FIELDS OF w_objdata
    FROM wwwdata
    WHERE srtf2 = 0 AND relid = 'MI' AND objid = p_objid.
    IF sy-subrc NE 0 OR w_objdata-objid EQ space.
      CONCATENATE '模板文件' w_objnam '不存在' INTO w_errtxt.
      MESSAGE w_errtxt TYPE 'I'.
    ENDIF.
    w_destination = p_dest.
    CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
      EXPORTING
        key         = w_objdata
        destination = w_destination
      IMPORTING
        rc          = w_rc.
    IF w_rc NE 0.
      CONCATENATE '模板文件：' w_objnam '下载失败' INTO w_errtxt.
      MESSAGE w_errtxt TYPE 'E'.
    ENDIF.
  ENDIF.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form upload_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM upload_data TABLES pt_file TYPE STANDARD TABLE
        USING p_file.

  DATA: wa_raw  TYPE truxs_t_text_data,
        li_line TYPE sy-tabix,
        lv_file TYPE rlgrap-filename,
        lv_str  TYPE string.
  DATA:lt_excel TYPE TABLE OF alsmex_tabline.
  DATA ls_excel LIKE LINE OF lt_excel.
  FIELD-SYMBOLS:<fs>.

  lv_file = p_file.

  CLEAR:pt_file,pt_file[].


  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = lv_file
      i_begin_col             = 1
      i_begin_row             = 4
      i_end_col               = 99
      i_end_row               = 9999
    TABLES
      intern                  = lt_excel
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

  LOOP AT lt_excel INTO ls_excel.
    ASSIGN COMPONENT ls_excel-col OF STRUCTURE gs_record TO <fs>.
    CONDENSE ls_excel-value.                  " 去掉空格
    TRANSLATE ls_excel-value TO UPPER CASE."转换大写
    <fs> = ls_excel-value.
    AT END OF row.
      "gs_tab-row = ls_excel-row.
      MOVE-CORRESPONDING gs_record TO gs_out.

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          input  = gs_out-kostl
        IMPORTING
          output = gs_out-kostl.

      APPEND gs_out TO gt_out.
      CLEAR gs_out.
      CLEAR: gs_record.
    ENDAT.
  ENDLOOP.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form disp_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM disp_alv .
  DATA: l_user_command  TYPE slis_formname,
        l_pf_status_set TYPE slis_formname.

  CLEAR it_fieldcat.REFRESH it_fieldcat.

  PERFORM get_catlog.

  l_user_command  = 'USER_COMMAND'.
  l_pf_status_set =  'SET_PF_STATUS'.

  is_layout-zebra = 'X'.
  is_layout-box_fname = 'SEL'.
  is_layout-cwidth_opt = 'X'.

  g_repid = sy-repid.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_interface_check        = ' '
      i_buffer_active          = ' '
      i_callback_pf_status_set = l_pf_status_set
      i_callback_user_command  = l_user_command
      i_callback_program       = g_repid
      is_layout_lvc            = is_layout      "it_events = lt_event
      it_fieldcat_lvc          = it_fieldcat[]
      i_save                   = 'A'
    TABLES
      t_outtab                 = gt_out[].
ENDFORM.

FORM set_pf_status USING pwa_extab TYPE slis_t_extab.
  SET PF-STATUS 'ZSTATUS' .
ENDFORM.

FORM user_command USING pa_ucomm LIKE sy-ucomm
      pwa_selfield TYPE slis_selfield.

  DATA:
    ls_layout TYPE lvc_s_layo,
    lr_grid   TYPE REF TO cl_gui_alv_grid.
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lr_grid.                   "
  CALL METHOD lr_grid->check_changed_data."

  CASE pa_ucomm.
    WHEN 'ZUP'. " 模拟导入
      PERFORM simulation_import_data.
      lv_x = 'X'.
    WHEN 'ZUPLOAD'. " 正式导入
      PERFORM formal_import_data.
    WHEN '&IC1'.
    WHEN 'OTHER'.
  ENDCASE.

  pwa_selfield-refresh = 'X'.
ENDFORM.                    "user_command

*&---------------------------------------------------------------------*
*& Form get_catlog
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM get_catlog .
  DEFINE lit_fieldcat. "
    it_fieldcat-tabname  = 'GT_OUT'.
    it_fieldcat-fieldname = &1.
    it_fieldcat-coltext = &2.
    it_fieldcat-just = &3.
    it_fieldcat-ref_field = &4.
    it_fieldcat-ref_table = &5.

    APPEND  it_fieldcat.
    CLEAR  it_fieldcat.
  END-OF-DEFINITION.   "lit_fieldcat

  lit_fieldcat :
  'TYPE'  '状态'   'C' '' '' ,
  'MESSAGE' '消息' 'L' '' '',
  'BELNR'  '凭证编号'    'L' 'BELNR'  'BSEG' ,
  'HEADID' '凭证序号'    'L' ''  '',
  'BUKRS' '公司代码'     'L' 'BUKRS'  'BSEG',
  'BLART' '凭证类型'     'L' ''  '',
  'BLDAT' '凭证日期'     'L' 'DATUM'  'SYST',
  'BUDAT' '过账日期'     'L' 'DATUM'  'SYST',
  'MONAT' '期间'         'L' ''  '',
  'WAERS' '货币'         'L' 'WAERS'  'T001',
  'XBLNR' '参考凭证号'   'L' ''  '',
  'NUMPG' '附件张数'     'L' ''  '',
  'BKTXT' '凭证抬头文本' 'L' ''  '',
  'BSCHL' '记帐代码'     'L' 'BSCHL'  'BSEG',
  'NEWKO' '屏幕科目'     'L' 'PARTNER'  'BUT000',
  'UMSKZ' '特别总帐标识' 'L' 'UMSKZ'  'BSEG',
  'HKONT' '总账科目'     'L' 'HKONT'  'BSEG',
  'MWSKZ' '税码'         'L' '' '',
  'HWBAS' '计税基础'     'L' '' '',
  'WRBTR' '凭证货币金额' 'L' 'WRBTR'  'BSEG',
  'DMBTR' '按本位币计的金额'  'L' 'DMBTR'  'BSEG',
  'KOSTL' '成本中心编号' 'L' 'KOSTL'  'BSEG',
  'AUFNR' '订单号'       'L' 'AUFNR'  'BSEG',
  'MATNR' '物料'         'L' 'MATNR'  'MARA',
  'WERKS' '工厂'         'L' 'WERKS'  'MARC',
  'MENGE' '数量'         'L' 'MENGE'  'BSEG',
  'MEINS' '单位'         'L' 'MEINS'  'BSEG',
  'ZFBDT' '基准日期'     'L' 'DATUM'  'SYST',
  'ZUONR' '分配'         'L' ''  '',
  'SGTXT' '项目文本'     'L' 'SGTXT'  'BSEG',
  'RSTGR' '原因代码'     'L' 'RSTGR'  'BSEG',
  'XNEGP' '反记账标识'   'L' 'XNEGP'  'BSEG'.

ENDFORM.